package com.zxc.controller.apply;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxc.model.Apply.ApplyWarehouse;
import com.zxc.model.Equipment.EquipmentApply;
import com.zxc.model.Page;
import com.zxc.service.ApplyService;
import com.zxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@CrossOrigin
@ResponseBody
@RequestMapping("/apply")
public class applyController {

    @Autowired
    private UserService userService;

    @Autowired
    private ApplyService applyService;
//这是申请填申请订单
    @RequestMapping("/setApplyDate")
    public ResponseEntity<?> setApplyDate(@RequestBody ApplyWarehouse applyWarehouse) {
//        获取到的是账号，通过账号在解析成他的id
        String userid=userService.getUser(applyWarehouse.getPageList().get(0).getAccount()).getId();
        applyWarehouse.setUserId(userid);
        return ResponseEntity.ok(applyService.setApplyDate(applyWarehouse));
    }
//获取申请记录
    @RequestMapping("/getApplyDate")
    public ResponseEntity<?> getApplyDate(@RequestBody ApplyWarehouse applyWarehouse) {
//        获取到的是账号，通过账号在解析成他的id
//        获取后来判断查询的数据库是root还是user
//        这里的判断其实要在service层执行的
        Page page =applyWarehouse.getPageList().get(0);
        if ("root".equals(applyWarehouse.getPageList().get(0).getAccount())){
            PageHelper.startPage(page.getOffset(), page.getLimit());
            return ResponseEntity.ok(new PageInfo<ApplyWarehouse>(applyService.getApplyDate(applyWarehouse)));
        }else {
//            如果是用户。查看到的是自己的审批记录。所以需要用userId去区分
            String id =userService.getUser(page.getAccount()).getId();
            applyWarehouse.setUserId(id);
            PageHelper.startPage(page.getOffset(), page.getLimit());
            return ResponseEntity.ok(new PageInfo<ApplyWarehouse>(applyService.getApplyDate(applyWarehouse)));
        }

    }
    @RequestMapping("/changeApplyState")
    public ResponseEntity<?> changeApplyState(@RequestBody ApplyWarehouse applyWarehouse) {
//        审批
        return ResponseEntity.ok(applyService.changeApplyState(applyWarehouse));
    }

    @RequestMapping("/changeEquipmentApplyState")
    public ResponseEntity<?> changeEquipmentApplyState(@RequestBody EquipmentApply equipmentApply) {
//        审批
        return ResponseEntity.ok(applyService.changeEquipmentApplyState(equipmentApply));
    }


}

